package com.zegome.game.gomestory.main.helper.network;

import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;

import android.os.AsyncTask;

public class AsyncTaskNetWork {
		
	INetWorkAction action;
	IGetSession session;
	IByteAsync bytesAction;
	HttpClient httpClient;
	HttpPost httpPost;
	HttpGet httpGet;
	
	
	public AsyncTaskNetWork(HttpClient httpClient, INetWorkAction action) {
		this.httpClient = httpClient;
		this.action = action;
	}
	
	public void setSession(IGetSession session){
		this.session = session;
	}
	
	public void setByteAction(IByteAsync action){
		this.bytesAction = action;
	}
	
	public void sendGetSession(){
		new RequestSession().execute("");
	}
	
	public void send(){
		new RequestTask().execute("");	
	}
	
	public void sendByte(){
		new RequestBytes().execute("");		
	}
	
	class RequestTask extends AsyncTask<String, String, String>{

	    @Override
	    protected String doInBackground(String... uri) {			
	    	return action.send();				
	    }

	    @Override
	    protected void onPostExecute(String result) {
	        super.onPostExecute(result);
	        action.receive(result);
	    }
	}
	
	class RequestSession extends AsyncTask<String, HttpResponse, HttpResponse>{

	    @Override
	    protected HttpResponse doInBackground(String... uri) {
	    	return session.send();				
	    }

	    @Override
	    protected void onPostExecute(HttpResponse result) {
	        super.onPostExecute(result);
	        session.receive(result);
	    }
	}
	
	class RequestBytes extends AsyncTask<String, byte[], byte[]>{

	    @Override
	    protected byte[] doInBackground(String... uri) {
	    	return bytesAction.send();
	    }

	    @Override
	    protected void onPostExecute(byte[] result) {
	        super.onPostExecute(result);
	        bytesAction.receive(result);
	    }
	}
}
